package com.df.tank;

import com.df.tank.strategy.DefaultFireStrategy;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

/**
 * 窗口类
 */
public class TankFrame extends Frame {
    static final int WIDTH = 800;
    static final int HEIGHT = 500;
    Tank tank = new Tank(200, 200, Dir.DOWN, Group.GOOD, this);
    List<Bullet> bulletList = new ArrayList<>();
    List<Tank> enemies = new ArrayList<>();
    List<Explode> explodes = new ArrayList<>();
    Award award = new Award(this);
    public TankFrame() {
        //设置可见
        setVisible(true);
        //设置是否可调大小
        setResizable(true);
        //设置大小
        setSize(WIDTH, HEIGHT);
        //设置标题
        setTitle("tank war");
        //添加监听器
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        addKeyListener(new MyKeyListener());
    }
    Image offScreenImage = null;

    @Override
    public void update(Graphics g) {
        if (offScreenImage == null) {
            offScreenImage = this.createImage(WIDTH, HEIGHT);
        }
        Graphics gOffScreen = offScreenImage.getGraphics();
        Color c = gOffScreen.getColor();
        gOffScreen.setColor(Color.GRAY);
        gOffScreen.fillRect(0, 0, WIDTH, HEIGHT);
        gOffScreen.setColor(c);
        paint(gOffScreen);
        g.drawImage(offScreenImage, 0, 0, null);
    }

    @Override
    public void paint(Graphics g) {
        g.drawString("子弹数量:"+bulletList.size(),10,60);
        g.drawString("敌方坦克数量:"+enemies.size(),10,80);
        if (enemies.isEmpty()) {
            g.drawImage(ImageMgr.endPics[0], 150, 100, null);
        }
        tank.paint(g);
        for (int i = 0; i < bulletList.size(); i++) {
            bulletList.get(i).paint(g);
        }
        for (int i = 0; i < enemies.size(); i++) {
            enemies.get(i).paint(g);
        }
        for (int i = 0; i < enemies.size(); i++) {
            Tank tank = enemies.get(i);
            for (int i1 = 0; i1 < bulletList.size(); i1++) {
                Bullet bullet = bulletList.get(i1);
                bullet.crashWith(tank);
                bullet.crashWith(this.tank);
            }
        }

        for (int i = 0; i < explodes.size(); i++) {
            explodes.get(i).paint(g);
        }

        if (award != null) {
            award.paint(g);
            award.getAward(tank);
        }

    }

    class MyKeyListener extends KeyAdapter {
        boolean bL = false;
        boolean bR = false;
        boolean bU = false;
        boolean bD = false;

        @Override
        public void keyPressed(KeyEvent e) {
            int keyCode = e.getKeyCode();
            switch (keyCode) {
                case KeyEvent.VK_LEFT:
                    bL = true;
                    break;
                case KeyEvent.VK_RIGHT:
                    bR = true;
                    break;
                case KeyEvent.VK_UP:
                    bU = true;
                    break;
                case KeyEvent.VK_DOWN:
                    bD = true;
                    break;
                default:
                    break;
            }
            setMainTankDir();
        }

        @Override
        public void keyReleased(KeyEvent e) {
            int keyCode = e.getKeyCode();
            switch (keyCode) {
                case KeyEvent.VK_LEFT:
                    bL = false;
                    break;
                case KeyEvent.VK_RIGHT:
                    bR = false;
                    break;
                case KeyEvent.VK_UP:
                    bU = false;
                    break;
                case KeyEvent.VK_DOWN:
                    bD = false;
                    break;
                case KeyEvent.VK_CONTROL:
                    tank.fire();
                    break;
                default:
                    break;
            }
            setMainTankDir();
        }

        private void setMainTankDir() {
            if (bL || bR || bU || bD) {
                tank.setMoving(true);
            }else {
                tank.setMoving(false);
            }
            if (bL) {
                tank.setDir(Dir.LEFT);
            }
            if (bR) {
                tank.setDir(Dir.RIGHT);
            }
            if (bU) {
                tank.setDir(Dir.UP);
            }
            if (bD) {
                tank.setDir(Dir.DOWN);
            }
        }
    }

    public Tank getTank() {
        return tank;
    }

    public void setTank(Tank tank) {
        this.tank = tank;
    }

    public List<Bullet> getBulletList() {
        return bulletList;
    }

    public void setBulletList(List<Bullet> bulletList) {
        this.bulletList = bulletList;
    }

    public List<Tank> getEnemies() {
        return enemies;
    }

    public void setEnemies(List<Tank> enemies) {
        this.enemies = enemies;
    }

    public List<Explode> getExplodes() {
        return explodes;
    }

    public void setExplodes(List<Explode> explodes) {
        this.explodes = explodes;
    }
}
